package com.tesco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tesco.dao.YgUserDao;
import com.tesco.entity.domin.YgUser;
import com.tesco.service.YgUserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;


@Service
public class YgUserServiceImpl extends ServiceImpl<YgUserDao, YgUser> implements YgUserService {


    @Override
    public Boolean userRegisterInterface(String username, String password,String usertype) {
        QueryWrapper<YgUser> wrapper = new QueryWrapper<>();
        wrapper.eq(YgUser.COL_USERNAME,username);
        YgUser one = this.getOne(wrapper);
        if (one == null){
            YgUser ygUser = new YgUser();
            ygUser.setUsername(username);
            ygUser.setPassword(password);
            ygUser.setUsertype(usertype);
            ygUser.setBalance(new BigDecimal(0));
            ygUser.setCreatedat(new Date());
            this.save(ygUser);
            return true;
        }
        return false;
    }

    @Override
    public YgUser userLoginInterface(String username, String password) {
        QueryWrapper<YgUser> wrapper = new QueryWrapper<>();
        wrapper.eq(YgUser.COL_USERNAME,username)
                .eq(YgUser.COL_PASSWORD,password);
        YgUser one = this.getOne(wrapper);
        return one;
    }

    @Override
    public YgUser userInfo(String username) {
        QueryWrapper<YgUser> ygUserQueryWrapper = new QueryWrapper<>();
        ygUserQueryWrapper.eq(YgUser.COL_USERNAME,username);
        YgUser one = this.getOne(ygUserQueryWrapper);
        return one;
    }

    @Override
    public void userUpdate(String username, YgUser ygUser) {
        YgUser ygUser1 = userInfo(username);
        ygUser.setHeadurl(ygUser1.getHeadurl());
        UpdateWrapper<YgUser> ygUserUpdateWrapper = new UpdateWrapper<>();
        ygUserUpdateWrapper.eq(YgUser.COL_USERNAME,username);
        this.update(ygUser,ygUserUpdateWrapper);
    }
}

